#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindowClass)
{
    ui->setupUi(this);

    addExec=new AddExec(this);
    connect(addExec, SIGNAL(execAdded(Exec*)), this, SLOT(execAdded(Exec*)));

    connect(ui->addButton, SIGNAL(clicked()), addExec, SLOT(show()));
    connect(ui->quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));

    ui->tableWidget->setColumnWidth(1, 80);

    createFileDialog();
}

void MainWindow::createFileDialog(){
    QFileDialog *fileDialog=new QFileDialog(this);
    connect(ui->toolButton, SIGNAL(clicked()), fileDialog, SLOT(exec()));
    connect(fileDialog, SIGNAL(fileSelected(QString)), ui->lineEdit, SLOT(setText(QString)));
}

MainWindow::~MainWindow(){
    delete ui;
}


void MainWindow::on_deleteButton_clicked(){
    int row=ui->tableWidget->currentRow();
    if(row==-1)
        return;

    ui->tableWidget->removeRow(row);
}

void MainWindow::execAdded(Exec *e){
    unsigned count=ui->tableWidget->rowCount();
    ui->tableWidget->setRowCount(count+1);

    QTableWidgetItem *file=new QTableWidgetItem(e->file().c_str());
    ui->tableWidget->setItem(count, 0, file);

    QTableWidgetItem *dir=new QTableWidgetItem(e->dir().c_str());
    ui->tableWidget->setItem(count, 2, dir);

    QTableWidgetItem *run=new QTableWidgetItem(e->execute()?"true":"false");
    ui->tableWidget->setItem(count, 1, run);
}

void MainWindow::on_bindButton_clicked(){
    unsigned count=ui->tableWidget->rowCount();
    if(!count){
        QMessageBox::information(this, "Information", "Select files to bind!");
        return;
    }

    QFile resource(":/files/stub");
    if(!resource.exists()){
        QMessageBox::critical(this, "Error", "Resource not found!");
        return;
    }

    QFile::remove(ui->lineEdit->text());
    resource.copy(ui->lineEdit->text());

    QFile output(ui->lineEdit->text());
    output.setPermissions(QFile::WriteOwner|QFile::ReadOwner|QFile::ExeOwner);
    output.open(QFile::Append);
    if(!output.isOpen()){
        QMessageBox::critical(this, "Error", "Could not create output file!");
        return;
    }

    for(unsigned i=0;i<count;i++){
        QFile file(ui->tableWidget->item(i, 0)->text());
        if(!file.open(QFile::ReadOnly)){
            QMessageBox::critical(this, "Error", "File number "+QString::number(i+1)+
                                  " is not readable or doesn't exist!");
            return;
        }
        if(ui->tableWidget->item(i, 2)->text().isEmpty()){
            QMessageBox::critical(this, "Error", "Choose extraction path for file number "+
                                  QString::number(i+1));
            return;
        }

        QByteArray buff=file.readAll();
        output.write("@)&#>^$%*<(@,,!~");
        std::string filepath=ui->tableWidget->item(i, 2)->text().toStdString();
        changeSeparators(filepath);

        output.write(filepath.c_str(), 128);
        char run=0;
        if(ui->tableWidget->item(i, 1)->text().compare("true", Qt::CaseInsensitive)==0)
            run=1;
        output.write(&run, 1);
        output.write(buff);
    }
    output.close();
}

void MainWindow::changeSeparators(std::string &filepath){
    for(unsigned i=0;i<filepath.size();i++)
        if(filepath[i]=='/')
            filepath[i]='\\';
}
